package tank02;

import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


import static tank02.MyGame.*;

/**
 * 坦克
 */
public class Tank {



    // x坐标
    private int x;
    // y坐标
    private int y;
    // 颜色
    private Color color;
    // 方向
    private int direction;
    //速度
    private int speed = 2;
    //子弹对象
//    private Bullet bullet = null;
    //子弹集合
    private ArrayList<Bullet> bullets = new ArrayList<>();
    //创建线程池对象 静态常量
    public static final ScheduledExecutorService threadPool =
            Executors.newScheduledThreadPool(50);
    public Tank(){}

    public Tank(int x, int y,Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    /**
     * 绘制坦克
     * @param g
     */
    public void draw(Graphics g){
        g.setColor(color);
        switch (direction){
            case DIRECTION_UP:
                //左履带
                g.fill3DRect(x,y,10,60,false);
                //车体
                g.fill3DRect(x + 10,y + 5,30,50,false);
                //右履带
                g.fill3DRect(x + 10 + 30, y, 10,60,false);
                //炮塔
                g.fillOval(x + 10,y + 5 + 10,30,30);
                //炮管
                g.fillRect(x + 10 + 15 - 3,y,6,15);
                break;
            case DIRECTION_DOWN:
                //左履带
                g.fill3DRect(x,y,10,60,false);
                //车体
                g.fill3DRect(x + 10,y + 5,30,50,false);
                //右履带
                g.fill3DRect(x + 10 + 30, y, 10,60,false);
                //炮塔
                g.fillOval(x + 10,y + 5 + 10,30,30);
                //炮管
                g.fillRect(x + 10 + 15 - 3,y + 5 + 10 + 30 + 1,6,15);
                break;
            case DIRECTION_LEFT:
                //右履带
                g.fill3DRect(x , y, 60,10,false);
                //车体
                g.fill3DRect(x + 5,y + 10,50,30,false);
                //左履带
                g.fill3DRect(x , y + 10 + 30, 60,10,false);
                //炮塔
                g.fillOval(x + 5 + 10,y + 10,30,30);
                //炮管
                g.fillRect(x,y + 10 + 15 - 3,15,6);
                break;
            case DIRECTION_RIGHT:
                //右履带
                g.fill3DRect(x , y, 60,10,false);
                //车体
                g.fill3DRect(x + 5,y + 10,50,30,false);
                //左履带
                g.fill3DRect(x , y + 10 + 30, 60,10,false);
                //炮塔
                g.fillOval(x + 5 + 10,y + 10,30,30);
                //炮管
                g.fillRect(x + 5 + 10 + 30 + 1,y + 10 + 15 - 3,15,6);
                break;
        }
        //遍历子弹集合
        for(int i = 0;i < bullets.size();i++){
            //获得每颗子弹并将子弹绘制出来
            Bullet bullet = bullets.get(i);
            if(bullet != null && !bullet.isRemoved()) {
                g.fillOval(bullet.getX(), bullet.getY(), 6, 6);
            }else {
                //如果子弹移除屏幕，就删除子弹
                bullets.remove(i);
            }
        }
    }



    /**
     * 发射子弹
     */
    public void fire(){
        Bullet bullet = null;
        //根据坦克方向创建子弹
        switch(direction){
            case DIRECTION_UP:
                bullet = new Bullet(x + 10 + 15 - 3,y,DIRECTION_UP);
                break;
            case DIRECTION_DOWN:
                bullet = new Bullet(x + 10 + 15 - 3,y + 60 - Bullet.HEIGHT,DIRECTION_DOWN);
                break;
            case DIRECTION_LEFT:
                bullet = new Bullet(x,y + 10 + 15 - 3,DIRECTION_LEFT);
                break;
            case DIRECTION_RIGHT:
                bullet = new Bullet(x + 60 - Bullet.HEIGHT,y + 10 + 15 - 3,DIRECTION_RIGHT);
                break;
        }
        //添加到子弹集合中
        bullets.add(bullet);
        //使用线程池来启动线程移动子弹
        threadPool.execute(bullet);
    }

    public void move(){
        switch (direction){
            case DIRECTION_UP:
                if (y > 0) {
                    y -= speed;
                }
                break;
            case DIRECTION_DOWN:
                if (y < PANEL_HEIGHT - 100) {
                    y +=speed;
                }
                break;
            case DIRECTION_LEFT:
                if (x > 0) {
                    x -= speed;
                }
                break;
            case DIRECTION_RIGHT:
                if (x < PANEL_HEIGHT - 80) {
                    x += speed;
                }
                break;
        }
    }
}

